<template>
  <div>
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>{{$route.meta.title}}</el-breadcrumb-item>
    </el-breadcrumb> -->
    <el-dialog :title="user.isAdd?'菜单添加':'菜单编辑'" :visible.sync="user.isshow" @closed="close">
      {{form}}
      <el-form :model="form">
        <el-form-item label="菜单名称" label-width="120px">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" label-width="120px">
          <el-select v-model="form.pid" placeholder="请选择上级菜单" @change="changePid">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option v-for="item in list" :key="item.id" :label="item.title" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单类型" label-width="120px">
          <el-radio :label="1" v-model="form.type" disabled>目录</el-radio>
          <el-radio :label="2" v-model="form.type" disabled>菜单</el-radio>
        </el-form-item>

        <el-form-item label="菜单图标" label-width="120px" v-if="form.type==1">
          <el-select v-model="form.icon" placeholder="请选择菜单图标">
            <el-option v-for="item in icons" :key="item" :value="item">
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单地址" label-width="120px" v-if="form.type==2">
          <el-select v-model="form.url" placeholder="请选择菜单地址">
            <el-option
              v-for="item in indexRouters"
              :key="item.path"
              :value="'/'+item.path"
            >{{item.meta.title}}</el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button v-if="user.isAdd" @click="add">添加</el-button>
        <el-button type="primary" v-else @click="edit">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { indexRouters } from "../../../router";
import { reqmenuAdd, reqmenuInfo, reqmenuEdit } from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["user", "list"],
  mounted() {
    // this.getOne();
  },
  data() {
    return {
      indexRouters,
      form: {
        pid: "", //顶级为0
        title: "",
        icon: "",
        type: "", //1是目录  2是菜单
        url: "", //'地址'
        status: "",
      },

      isshow: true,

      icons: [
        "el-icon-user-solid",
        "el-icon-setting",
        "el-icon-s-goods",
        "el-icon-s-help",
        "el-icon-camera-solid",
      ],
    };
  },
  methods: {
    //   关闭是否清空form
    close() {
      if (!this.user.isAdd) {
        this.empty();
      }
    },

    //修改
    edit() {
      reqmenuEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          //   取消form
          this.cancel();
          //   重新刷新数据
          this.$emit("init");
          //   修改后清空form
          this.empty();
        }
      });
    },

    //获取一条数据
    getOne(id) {
      reqmenuInfo({
        id: id,
      }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.id = id;
        }
      });
    },

    //   取消后form消失
    cancel() {
      this.user.isshow = false;
    },

    // 点击添加后
    add() {
      reqmenuAdd(this.form).then((res) => {
        if (res.data.code === 200) {
          successAlert(res.data.msg);
          //   弹窗消失
          this.cancel();
          //   清空
          this.empty();
          //   刷新列表
          this.$emit("init");
        }
      });
    },

    empty() {
      this.form = {
        pid: "", //顶级为0
        title: "",
        icon: "",
        type: "", //1是目录  2是菜单
        url: "", //'地址'
        status: "",
      };
    },
    changePid() {
      this.form.type = this.form.pid == 0 ? 1 : 2;
    },
  },
};
</script>

<style>
</style>